<?php
/**
 * Created by PhpStorm.
 * User: DQ
 * Date: 2018/8/30
 * Time: 20:14
 */
require 'function.php';

$data = file_get_contents('logs/log-20180828.txt');

$json_data = json_decode($data, true);
$requestData = h_property_exists($json_data, 'data');
$postData = [];

if ($requestData) {
    $data = h_property_exists($requestData, 'data');
    if ($data) {
        $rows = h_property_exists($data, 'rows');
        if ($rows) {
            foreach ($rows as $row) {

                $post = [];
                $post['state'] = 'amazon.' . h_property_exists($json_data, 'site');
                $post['FromDate'] = h_property_exists($json_data['time'], 'start');
                $post['ToDate'] = h_property_exists($json_data['time'], 'end');
                $post['pAsin'] = get_asin_or_sku_form_item($row[0]);
                $post['ASIN'] = get_asin_or_sku_form_item($row[1]);
                $post['Title'] = $row[2];
                $post['SKU'] = get_asin_or_sku_form_item($row[3]);
                $post['Sessions'] = $row[4];
                $post['SessionPercentage'] = get_percent_from_item($row[5]);
                $post['PageViews'] = $row[6];
                $post['PageViewsPercentage'] = get_percent_from_item($row[7]);
                $post['BuyBoxPercentage'] = get_percent_from_item($row[8]);
                $post['UnitsOrdered'] = $row[9];
                $post['UnitsOrderedB2B'] = $row[10];
                $post['UnitSessionPercentage'] = get_percent_from_item($row[11]);
                $post['UnitSessionPercentageB2B'] = get_percent_from_item($row[12]);
                $post['OrderedProductSales'] = get_money_from_item($row[13]);
                $post['OrderedProductSalesB2B'] = get_money_from_item($row[14]);
                $post['TotalOrderItems'] = $row[15];
                $post['TotalOrderItemsB2B'] = $row[16];

                $postData[] = $post;
            }
        }
    }
    success_response($postData);

}